<template>
  <div>
    <div class="ui container segment">
      <el-table :data="dealFileLists" style="width: 100%" v-loading="isloading" fit>
        <el-table-column prop="realName" label="文件名" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="uploadTime" :formatter="upDateFormatter" label="上传时间">
        </el-table-column>
        <el-table-column prop="approvalTime" :formatter="psDateFormatter" label="审批时间">
        </el-table-column>
        <el-table-column prop="description" label="描述" show-overflow-tooltip>
        </el-table-column>
        <el-table-column prop="code" label="文件密码">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-popconfirm title="删除文件后将会永久删除，是否继续？" @confirm="deleteDoc(scope.row)" icon-color="red">
              <el-button slot="reference" type="text">删除</el-button>
            </el-popconfirm>
            &nbsp;<el-button @click="shareDoc(scope.row)" type="text">分享</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分享弹框 -->
      <el-dialog title="分享文件" :visible.sync="shareDialog" width="30%" center>
        <el-input v-model="linkInput" :disabled="true" id="copyLink" width="70px">
          <el-button slot="append" icon="el-icon-copy" @click="copyLink" type="primary">复制</el-button>
        </el-input>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="shareDialog = false">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  inject: ['reload'],
  data() {
    return {
      isloading: false,         //加载
      dealFileLists: null,     //处理的文件
      shareDialog: false,        //分享弹框
      linkInput: '',   //分享链接
    }
  },
  methods: {
    deleteDoc(row) {
      const currentId = row.id;
      this.$https({
        method: 'delete',
        url: '/user/deleteFile',
        params: {
          fileId: currentId
        }
      }).then(resp => {
        this.reload();
        if (resp.data.code == 200)
          this.$message.success(resp.data.data);
        else
          this.$message.info(resp.data.data);
      })
    },
    shareDoc(row) {
      this.$https({
        method:'get',
        url:'/user/sharedLinks',
        params:{fileId:row.id}
      }).then(res=>{
        console.log(res);
        this.linkInput = res.data.data;
      })
      this.shareDialog = true;
    },
    copyLink() {
      this.$copyText(this.linkInput).then(() => {
        this.$message.success("复制成功！");
      },()=>{
        this.$message.error("出错啦！");
      })
    },
    getMyList() {
      this.$https.get('/user/myFileList').then((res) => {
        if (res.data.code == 200)
          this.dealFileLists = res.data.data.filter(e => { return e.status == 2 });
        else
          this.$message.info(res.data.message);
      })
    },
    upDateFormatter(row, column) {
      let datetime = row.uploadTime;
      return this.timestampToTime(datetime);
    },
    psDateFormatter(row, column) {
      let datetime = row.approvalTime;
      return this.timestampToTime(datetime);
    },
    timestampToTime(time) {
      //时间戳转换
      // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
      var date = new Date(time);
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      let d = date.getDate();
      let h = date.getHours();
      let m = date.getMinutes();
      let s = date.getSeconds();
      MM = MM < 10 ? "0" + MM : MM;
      d = d < 10 ? "0" + d : d;
      h = h < 10 ? "0" + h : h;
      m = m < 10 ? "0" + m : m;
      s = s < 10 ? "0" + s : s;
      // return y + "-" + MM + "-" + d + " " + h + ":" + m + ":" + s;
      return y + "-" + MM + "-" + d;
    },
  },
  created() {
    this.getMyList();
  }
}
</script>

<style scoped>
</style>